package com.wu.qqserver.service;

import com.wu.qqcommon.Message;
import com.wu.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/*
该类的一个对象和客户端持续保持通信
 */
public class ServerConnectClientThread extends Thread{

    private Socket socket;
    private String userId; //链接的用户id

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {

        while (true){//循环保持通讯
            try {
                System.out.println("服务器跟客户端"+userId+"保持通讯。。。。。。。");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message)ois.readObject();
                //根据客户端穿过来的消息类型，做相应的业务处理
                //如果客户端输入1，即显示在线用户列表，则是如下方法
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                    System.out.println("------"+message.getSenter()+"请求显示在线用户列表-----");
                    //我们直接返回就行了，写个方法在ManageServerConnectClientThread中
                    String onLineUserList = ManageServerConnectClientThread.getOnLineUserList();
                    //返回一个message
                    Message message1 = new Message();
                    message1.setContent(onLineUserList);
                    message1.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message1.setGetter(message.getSenter());
                    //写入到数据通道，返回给客户端
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message1);
                }else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                        //退出系统
                         System.out.println(message.getSenter()+"要退出系统了。。。。");
                         ManageServerConnectClientThread.logOutServerConnectClientThread(message.getSenter());
                         socket.close();//关闭链接
                         break;//退出while循环，结束链接
                    }else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)){
                            //首先先获得发给谁，这个谁的线程以及socket和对应的输出
                    System.out.println(message.getSenter()+"正在给："+message.getGetter()+"私发消息"+message.getContent());
                    //如果不在线
                    if (!ManageServerConnectClientThread.userIdIsOnLine(message.getGetter())){
                        OffLineNewsToOneUserService.addOffNews(message.getGetter(),message);
                    }//在线就执行else中  将原来的代码移到else中
                    else{
                        ObjectOutputStream oos = new ObjectOutputStream(
                                ManageServerConnectClientThread.
                                        getServerConnectClientThread(message.getGetter())
                                        .socket
                                        .getOutputStream());
                        message.setContent(message.getContent());
                        oos.writeObject(message);
                    }

                            }else if (message.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES)){
                    System.out.println(message.getSenter()+"正在--群发消息"+message.getContent());
                                //我要在ManageServerConnectClientThread.中写一个获取所有用户的方法all

                    message.setContent(message.getContent());
                        String allOnLineUserList = ManageServerConnectClientThread.getOnLineUserList();
                    String[] allUserId = allOnLineUserList.split(" ");
                    for (String s : allUserId) {
                        if (s.equals(message.getSenter())){
                            continue;
                        }
                        ObjectOutputStream oos = new ObjectOutputStream(ManageServerConnectClientThread
                                .getServerConnectClientThread(s)
                                .socket
                                .getOutputStream()
                        );
                        oos.writeObject(message);
                    }//传送文件的逻辑
                } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)){
                    ObjectOutputStream oos = new ObjectOutputStream(
                            ManageServerConnectClientThread.
                                    getServerConnectClientThread(message.getGetter())
                                    .socket
                                    .getOutputStream());
                    oos.writeObject(message);
                }  else{
                        System.out.println("其他的暂时不处理。。。。。");
                        }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


        }
    }
}
